package gestion.camping.vue;

import gestion.camping.controleur.SaisonController;
import gestion.camping.dto.Saison;
import gestion.camping.utils.DateFormatUtils;
import gestion.camping.utils.MessagesUtil;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class ViewSaisonListe extends JPanel implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = -1923064145354238975L;
	
	private TableSaison table;

	private List<Saison> listSaison;
	private SaisonController controller;
	private JButton btnRechercher;
	private JButton btnValider;
	private JButton btnAjouter;
	private JLabel lblDateDebut;
	private JTextField textDateDebut;
	private JLabel lblDateFin;
	private JTextField textDateFin;

	/**
	 * Create the panel.
	 */
	public ViewSaisonListe(SaisonController saisonController) {
	
		this.controller = saisonController;
		
		this.listSaison = saisonController.getListSaison();
		
		initCompoment();
	}
	
	public void initCompoment(){
	
		setBounds(0, 25, 1150, 700);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.15, 0.2, 1.0, 0.2, 1.0, 0.15};
		gridBagLayout.rowWeights = new double[]{0.1, 0.0, 0.0, 0.0, 0.0, 0.5, 0.1};
		setLayout(gridBagLayout);
		
		btnRechercher = new JButton(MessagesUtil.formatMessage("saison.liste.btn.rechercher"));
		btnRechercher.addActionListener(this);
		
		lblDateDebut = new JLabel(MessagesUtil.formatMessage("saison.liste.critere.dateDebut"));
		GridBagConstraints gbc_lblDateDebut = new GridBagConstraints();
		gbc_lblDateDebut.anchor = GridBagConstraints.EAST;
		gbc_lblDateDebut.insets = new Insets(0, 0, 5, 5);
		gbc_lblDateDebut.gridx = 1;
		gbc_lblDateDebut.gridy = 2;
		add(lblDateDebut, gbc_lblDateDebut);
		
		textDateDebut = new JTextField(controller.getDateDebutString());
		GridBagConstraints gbc_textDateDebut = new GridBagConstraints();
		gbc_textDateDebut.insets = new Insets(0, 0, 5, 5);
		gbc_textDateDebut.fill = GridBagConstraints.HORIZONTAL;
		gbc_textDateDebut.gridx = 2;
		gbc_textDateDebut.gridy = 2;
		add(textDateDebut, gbc_textDateDebut);
		textDateDebut.setColumns(10);
		
		lblDateFin = new JLabel(MessagesUtil.formatMessage("saison.liste.critere.dateFin"));
		GridBagConstraints gbc_lblDateFin = new GridBagConstraints();
		gbc_lblDateFin.anchor = GridBagConstraints.EAST;
		gbc_lblDateFin.insets = new Insets(0, 0, 5, 5);
		gbc_lblDateFin.gridx = 3;
		gbc_lblDateFin.gridy = 2;
		add(lblDateFin, gbc_lblDateFin);
		
		textDateFin = new JTextField(controller.getDateFinString());
		GridBagConstraints gbc_textDateFin = new GridBagConstraints();
		gbc_textDateFin.insets = new Insets(0, 0, 5, 5);
		gbc_textDateFin.fill = GridBagConstraints.HORIZONTAL;
		gbc_textDateFin.gridx = 4;
		gbc_textDateFin.gridy = 2;
		add(textDateFin, gbc_textDateFin);
		textDateFin.setColumns(10);
		GridBagConstraints gbc_btnRechercher = new GridBagConstraints();
		gbc_btnRechercher.anchor = GridBagConstraints.EAST;
		gbc_btnRechercher.insets = new Insets(0, 0, 5, 5);
		gbc_btnRechercher.gridx = 4;
		gbc_btnRechercher.gridy = 4;
		add(btnRechercher, gbc_btnRechercher);
		
		table = new TableSaison();
		
		GridBagConstraints gbc_table = new GridBagConstraints();
		gbc_table.gridwidth = 4;
		gbc_table.insets = new Insets(0, 0, 5, 5);
		gbc_table.fill = GridBagConstraints.BOTH;
		gbc_table.gridx = 1;
		gbc_table.gridy = 5;
		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane, gbc_table);
		
		btnAjouter = new JButton(MessagesUtil.formatMessage("saison.ajout.title"));
		btnAjouter.addActionListener(this);
		GridBagConstraints gbc_btnAjouter = new GridBagConstraints();
		gbc_btnAjouter.anchor = GridBagConstraints.EAST;
		gbc_btnAjouter.insets = new Insets(0, 0, 0, 5);
		gbc_btnAjouter.gridx = 3;
		gbc_btnAjouter.gridy = 6;
		add(btnAjouter, gbc_btnAjouter);
		
		btnValider = new JButton(MessagesUtil.formatMessage("saison.liste.btn.modifier"));
		btnValider.addActionListener(this);
		GridBagConstraints gbc_btnValider = new GridBagConstraints();
		gbc_btnValider.anchor = GridBagConstraints.WEST;
		gbc_btnValider.insets = new Insets(0, 0, 0, 5);
		gbc_btnValider.gridx = 4;
		gbc_btnValider.gridy = 6;
		add(btnValider, gbc_btnValider);
		
	}

	public List<Saison> getListSaison() {
		return listSaison;
	}

	public void setListSaison(List<Saison> listSaison) {
		this.listSaison = listSaison;
		
		update();
	}
	
	private void update(){
		int indexElementSelected = table.getSelectedRow();
		
		table.setModel(new TableSaisonModel(listSaison, table.getColumnNames()));
		table.setShowGrid(true);
		table.setVisible(true);
		
		this.repaint();
		
		if(indexElementSelected >= 0){
			table.setRowSelectionInterval(indexElementSelected, indexElementSelected);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnRechercher){
			this.controller.rechercher(DateFormatUtils.parseDate(textDateDebut.getText()), DateFormatUtils.parseDate(textDateFin.getText()));
		}else if(e.getSource() == btnValider){
			Saison selectedSaison = listSaison.get(this.table.getSelectedRow());			
			
			this.controller.modifier(selectedSaison);
		}else if(e.getSource() == btnAjouter){
			this.controller.ajouter();
		}
		
	}
	
	public Saison getSelectedSaison(){	
		return table.getSelectedRow() < 0 ? null : listSaison.get(table.getSelectedRow());
	}

}
